/**
 * ...
 * @author Thot
 */

function KeyboardTracker(){}


KeyboardTracker.KEY_DOWN 	= "keydown";
KeyboardTracker.KEY_UP 		= "keyup";

KeyboardTracker.prototype._element 	= null;
KeyboardTracker.prototype._signal 	= null;
KeyboardTracker.prototype._keyStates = null;


KeyboardTracker.prototype.listen = function()
{
	var _context = this;
	var callBack = function(event)
	{
		_context.keyHandler.apply( _context, [event] );
	};
	
	document.onkeyup 	= callBack;
	document.onkeydown 	= callBack;
	
	this.signal = new Signal();
	this._keyStates = new Object();
};


KeyboardTracker.prototype.keyHandler = function(event)
{
	var character = String.fromCharCode(event.keyCode).toLowerCase();
	var state = 0;
	
	switch( event.type )
	{
		case "keyup":		
			state = 0; 
			this.signal.dispatchEvent( KeyboardTracker.KEY_UP, character ); 		
			break;
			
		case "keydown":		
			state = 1; 
			this.signal.dispatchEvent( KeyboardTracker.KEY_DOWN, character ); 	
			break;
	}
	
	this._keyStates[character] = state;
};


KeyboardTracker.prototype.getKeyState = function( p_key )
{
	if( this._keyStates[p_key] )
	{
		return this._keyStates[p_key];
	}
	else
	{
		return 0;
	}
};
